package com.lute.model;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import com.lute.utils.DAOFactory;

@Entity
public class EmployeeProfile {
	
	private int id_employee_profile;
	private Employee employee;
	private Approver approver;
	private Integer employeeApprover; 
	private Client client;
	private BusinessUnit businessUnit;
	private String employmentStatus;
	private Position position;
	private GregorianCalendar startWorking;
	private GregorianCalendar workLifeLine;
	private GregorianCalendar billingStartDate;
	private GregorianCalendar backgroundCheck;
	private String email;
	private String note;
	private String employeeCurrentState = EmployeeCurrentState.NOT_CONFIGURED.toString();
	

	public EmployeeProfile() {}
	public EmployeeProfile(Employee employee) {
		this.employee = employee;
	}
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	public int getId_employee_profile() {
		return id_employee_profile;
	}
	public void setId_employee_profile(int id_employee_profile) {
		this.id_employee_profile = id_employee_profile;
	}
	
	@OneToOne
	@JoinColumn(name="id_employee")
	public Employee getEmployee() {
		return employee;
	}
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}
	@OneToOne
	@JoinColumn(name="id_client")
	public Client getClient() {
		return client;
	}
	public void setClient(Client client) {
		this.client = client;
	}
	
	@OneToOne
	@JoinColumn(name="id_approver")
	public Approver getApprover() {
		return approver;
	}
	public void setApprover(Approver approver) {
		this.approver = approver;
	}
	
	public Integer getEmployeeApprover() {
		return employeeApprover;
	}
	public void setEmployeeApprover(Integer employeeApprover) {
		this.employeeApprover = employeeApprover;
	}
	
	@OneToOne
	@JoinColumn(name="id_businessUnit")
	public BusinessUnit getBusinessUnit() {
		return businessUnit;
	}
	public void setBusinessUnit(BusinessUnit businessUnit) {
		this.businessUnit = businessUnit;
	}
	public String getEmploymentStatus() {
		return employmentStatus;
	}
	public void setEmploymentStatus(String employmentStatus) {
		this.employmentStatus = employmentStatus;
	}
	@OneToOne
	@JoinColumn(name="id_position")
	public Position getPositions() {
		return position;
	}
	public void setPositions(Position positions) {
		this.position = positions;
	}
	public GregorianCalendar getStartWorking() {
		return startWorking;
	}
	public void setStartWorking(GregorianCalendar startWorking) {
		this.startWorking = startWorking;
	}
	public GregorianCalendar getWorkLifeLine() {
		return workLifeLine;
	}
	public void setWorkLifeLine(GregorianCalendar workLifeLine) {
		this.workLifeLine = workLifeLine;
	}
	public GregorianCalendar getBillingStartDate() {
		return billingStartDate;
	}
	public void setBillingStartDate(GregorianCalendar billingStartDate) {
		this.billingStartDate = billingStartDate;
	}
	public GregorianCalendar getBackgroundCheck() {
		return backgroundCheck;
	}
	public void setBackgroundCheck(GregorianCalendar backgroundCheck) {
		this.backgroundCheck = backgroundCheck;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getNote() {
		return note;
	}
	public void setNote(String note)  {
		this.note = note;
	}
	public void appendNote(String note) {
		this.note = this.note + ";" + note;
	}
	
	public String getEmployeeCurrentState() {
		return employeeCurrentState;
	}
	public void setEmployeeCurrentState(String employeeCurrentState) {
		this.employeeCurrentState = employeeCurrentState;
	}
	
	public static String getEmployeeCurrentState(String propertyName, int propertyValue) {
		EmployeeProfile employeeProfile =  getEmployeeProfileByEmployeeIdFromDB(propertyName,propertyValue);
		return employeeProfile.getEmployeeCurrentState();
	}
	
	public static int getEmployeeClientId(String propertyName, int propertyValue) {
		EmployeeProfile employeeProfile =  getEmployeeProfileByEmployeeIdFromDB(propertyName,propertyValue);
		return employeeProfile.getClient().getId_client();
	}
	
	public static void updateEmployeeProfileInDB(EmployeeProfile employeeProfile) {
		DAOFactory.getInstance().getLuteInterface().updateObjectinDataBase(employeeProfile);
	}
	
	public static EmployeeProfile getEmployeeProfileFromDB(String propertyName, int propertyValue) {
		return DAOFactory.getInstance().getLuteInterface().getEmployeeProfileById(propertyName, propertyValue);
	}
	
	public static EmployeeProfile getEmployeeProfileByEmployeeIdFromDB(String employeeRole, int userId) {
		return DAOFactory.getInstance().getLuteInterface().getEmployeeProfileByEmployeeIdFromDataBase(employeeRole, userId);
	}
	
	public static List<EmployeeProfile> getEmployeeProfilesBySpecifiedClientIdFromDB(int clientId) {
		return DAOFactory.getInstance().getLuteInterface().getEmployeeProfileByClientIdFromDataBase("client.id", clientId);
	}
	
	public static List<EmployeeProfile> getEmployeeProfilesBySpecifiedUserFromDB(String userRole) {
		List<EmployeeProfile> profiles = new ArrayList<EmployeeProfile>();
		
		if(userRole.equals("approver")) {
			List<Approver> approvers = Approver.getApproversFromDB(); 
			for(Approver approver: approvers) {
				profiles.add(approver.getApproverProfile());
			}
		} else {
			List<Employee> employees = Employee.getEmployeesFromDB(); 
			profiles = Employee.getEmployeeProfiles(employees);
		}
		return profiles;
	}
	
	public static List<EmployeeProfile> getEmployeeProfilesByUserRoleWithAccLockedFromDB(String propertyName, String propertyValue, String userRole) {
		List<EmployeeProfile> profiles = new ArrayList<EmployeeProfile>();
		
		if(userRole.equals("approver")) {
			List<Approver> approvers = Approver.getApproversByAccLockedFromDB(propertyName, propertyValue); 
			for(Approver approver: approvers) {
				profiles.add(approver.getApproverProfile());
			}
		} else {
			List<Employee> employees = Employee.getEmployeesByAccLockedFromDB(propertyName, propertyValue); 
			profiles = Employee.getEmployeeProfiles(employees);
		}
		return profiles;
	}
	
	public static List<EmployeeProfile> getEmployeeProfilesByAccLockedFromDB(String propertyName, String propertyValue) {
		List<User> users = User.getEmployeesAndApproversFromDB(propertyName, propertyValue);
		List<Integer> ids = new ArrayList<Integer>();
		for(User user: users) {
			ids.add(user.getId_user());
		}
		return DAOFactory.getInstance().getLuteInterface().getEmployeeProfilesByUserIdsFromDataBase(ids);
	}
	
	public static List<EmployeeProfile> getEmployeeProfilesFromDB() {
		return DAOFactory.getInstance().getLuteInterface().getEmployeeProfilesFromDataBase();
	}
}
